/*
 * @Author: 张书源 526384196@qq.com
 * @Date: 2023-11-19 09:07:07
 * @LastEditors: 张书源 526384196@qq.com
 * @LastEditTime: 2023-11-19 09:07:47
 * @FilePath: \blog-admin\src\api\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Axios from 'axios';

// 根据环境变量进行变量的更改
let baseURL = '';
const env = import.meta.env.VITE_APP_DEV
if(env === 'dev') { // 开发环境
    baseURL = import.meta.env.VITE_APP_URL;
}else if(env === 'prd') {
    baseURL = import.meta.env.VITE_APP_URL;
}

const axios = Axios.create({
    baseURL,
    timeout: 20000 // 请求超时 20s
})

// 请求拦截器
axios.interceptors.request.use(
    (response) => {

    },
    (error) => {

    }
)

// 响应拦截器
axios.interceptors.response.use(
    (response) => {

    },
    (error) => {

    }
)

export function request(option) {
    if(option.method === 'get') {
        return new Promise((resolve, reject) => {
            axios({
                method: option.method,
                url: option.url,
                params: option.params,
                headers: {
                    'content-type': 'application/json'
                }
            }).then((res) => {
                resolve(res);
            }).catch((err) => {
                reject(err);
            })
        });
    }else if(option.method === 'post'){
        return new Promise((resolve, reject) => {
            axios({
                    method: option.method,
                    url: option.url,
                    data: option.data,
                    headers: {
                        'content-type': 'application/json'
                    }
            }).then((res) => {
                resolve(res);
            }).catch((err) => {
                reject(err);
            })
        });
    }
}

export default request;